package edu.towson.cosc.cosc455.pjemmott;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;

import java.util.Scanner;
import java.util.Stack;

public class MyLexicalAnalyzer implements LexicalAnalyzer {
	static String token2;
	static Scanner scans;
	static Stack s = new Stack();

	@Override
	public String getNextToken(String token) throws CompilerException {

		token2 = "";
		scans = new Scanner(token);
		
		//This block deletes whitespace (tabs and spaces) before each token if it exists
		int v = 0;
		if (isSpace(mycompiler.token, 0) == true
				|| mycompiler.token.charAt(0) == '\t') {

			while ((isSpace(mycompiler.token, v) == true || mycompiler.token
					.charAt(v) == '\t')
					&& !mycompiler.token.equalsIgnoreCase("#END DOCUMENT")) {
				v++;
				// System.out.println(mycompiler.token);

			}
			String nexttoken = "";
			mycompiler.token = mycompiler.token.substring(nexttoken.length()
					+ v, mycompiler.token.length());
		}
		//end block

		
		String nexttoken = scans.next();
		//if the next token is a tag
		if (nexttoken.charAt(0) == '#') {
			mycompiler.token = mycompiler.token.substring(nexttoken.length(),
					mycompiler.token.length());

			for (int i = 0; i < nexttoken.length(); i++) {

				token2 += getCharacter(nexttoken, i);

			}

			//if the nexttoken is a single word tag
			if (token2.compareToIgnoreCase("#NEWLINE") == 0
					|| token2.compareToIgnoreCase("#NAME") == 0
					|| token2.compareToIgnoreCase("#VALUE") == 0) {

				//otherwise the tag has 2 parts and get the second half
			} else {
				nexttoken = " " + scans.next();

				for (int i = 0; i < nexttoken.length(); i++) {
					token2 += getCharacter(nexttoken, i);

				}

			
				//checks to see if the token is legal
				if (lookupToken(token2) == false) {
					String message = "incorrect TAG! " + token2;

					throw new CompilerException(message);

				}
				//removes token from File String
				mycompiler.token = mycompiler.token.substring(
						nexttoken.length(), mycompiler.token.length());
				
				s.push(token2);
				return token2;
			}

		}
		//if the token is text keep getting it until a tag is next
		if (nexttoken.charAt(0) != '#') {

			while (nexttoken.charAt(0) != '#') {

				for (int t = 0; t < nexttoken.length(); t++) {

					token2 += getCharacter(nexttoken, t);
				}
				token2 += " ";
				nexttoken = scans.next();

			}
			//removes text token from File String
			mycompiler.token = mycompiler.token.substring(token2.length(),
					mycompiler.token.length());

		}
		s.push(token2);

		return token2;
	}



	//gets and returns the character at the position of the input string
	@Override
	public char getCharacter(String token2, int n) {
		return token2.charAt(n);
	}

	
//checks for a space between tokens at the given point in the String
	@Override
	public boolean isSpace(String c, int m) {
		if (c.charAt(m) == (' '))
			return true;
		else
			return false;
	}

	//list of legal Tags
	@Override
	public boolean lookupToken(String token) {
		String[] array = { "#START DOCUMENT", "#END DOCUMENT", "#START HEAD",
				"#END HEAD", "#START TITLE", "#END TITLE", "#START PARAGRAPH",
				"#END PARAGRAPH", "#START BOLD", "#END BOLD", "#START ITALICS",
				"#END ITALICS", "#START VIDEO", "#END VIDEO", "#START AUDIO",
				"#END AUDIO", "#START ITEM", "#END ITEM", "#START USE",
				"#END USE", "#START LIST", "#END LIST", "#START DEFINE",
				"#END DEFINE", "#NAME", "#NEWLINE" };
		for (int i = 0; i < array.length; i++) {
			if (token.equalsIgnoreCase(array[i]))
				return true;

		}
		return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}


}
